package com.gewu.pm.ui.adapter.delivery;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.gewu.pm.R;
import com.gewu.pm.app.AppAdapter;

/**
 * 配送商对账列表适配器
 */
public class DeliveryProviderReconciliationAdapter extends AppAdapter<String> {

    private int mReconciliationType;  // 对账类型（0=待确认，1=已确认）
    private OnActionListener mActionListener;

    public DeliveryProviderReconciliationAdapter(Context context, int reconciliationType) {
        super(context);
        this.mReconciliationType = reconciliationType;
    }

    /**
     * 设置操作监听器
     */
    public void setOnActionListener(OnActionListener listener) {
        this.mActionListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder();
    }

    private final class ItemViewHolder extends AppAdapter<?>.ViewHolder {

        private final TextView mDateRangeView;
        private final TextView mUploadInvoiceButton;
        private final TextView mViewInvoiceButton;

        private ItemViewHolder() {
            super(R.layout.delivery_provider_reconciliation_item);
            mDateRangeView = findViewById(R.id.tv_date_range);
            mUploadInvoiceButton = findViewById(R.id.btn_upload_invoice);
            mViewInvoiceButton = findViewById(R.id.btn_view_invoice);

            // 上传发票按钮点击
            mUploadInvoiceButton.setOnClickListener(v -> {
                if (mActionListener != null) {
                    mActionListener.onUploadInvoice(getViewHolderPosition());
                }
            });

            // 查看发票按钮点击
            mViewInvoiceButton.setOnClickListener(v -> {
                if (mActionListener != null) {
                    mActionListener.onViewInvoice(getViewHolderPosition());
                }
            });
        }

        @Override
        public void onBindView(int position) {
            String dateRange = getItem(position);

            // 设置日期范围
            mDateRangeView.setText(dateRange);

            // 根据对账类型显示不同的按钮
            if (mReconciliationType == 0) {
                // 待确认：显示上传发票按钮
                mUploadInvoiceButton.setVisibility(View.VISIBLE);
                mViewInvoiceButton.setVisibility(View.GONE);
            } else {
                // 已确认：显示查看发票按钮
                mUploadInvoiceButton.setVisibility(View.GONE);
                mViewInvoiceButton.setVisibility(View.VISIBLE);
            }
        }
    }

    /**
     * 操作监听接口
     */
    public interface OnActionListener {
        /**
         * 上传发票
         */
        void onUploadInvoice(int position);

        /**
         * 查看发票
         */
        void onViewInvoice(int position);
    }
}

